<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 树形控件主体区域 -->
            <el-tree
              class="departments-tree"
              :data="treeData"
              :props="defaultProps"
              default-expand-all
            >
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDept(data.id)">添加部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 添加/编辑部门弹框 -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '添加部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 添加子组件弹层 -->
          <!-- v-if用来处理回显数据在created中只执行一次的bug -->
          <!-- 每次关闭弹框都将其销毁 再点击弹框时就会再在created中重新执行一次获取新的数据回显 -->
          <deptDialog
            v-if="showDialog"
            :pid="pid"
            :is-edit="isEdit"
            :origin-list="originList"
            @update-depart="updateDept"
            @close="showDialog=false"
          />

        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { transListToTree } from '@/utils'
import deptDialog from './deptDialog'
import { delDepartment } from '@/api/departments'
export default {
  name: 'Departments',
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      treeData: [
        {
          name: '财务部',
          children: [
            {
              name: '财务核算部'
            }
          ]
        }
      ],
      defaultProps: {
        // 表示下一级
        children: 'children',
        // 要显示内容的键
        label: 'name'
      },
      showDialog: false,
      pid: '', // 部门id
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async delDept(id) {
      // confir 询问框  它是一个promise对象需要加await
      const confirmRes = await this.$confirm('是否要删除该部门', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 判断点击的取消 后的提示
      if (confirmRes === 'cancel') return this.$message('已取消了删除')
      // 执行删除
      const res = await delDepartment(id)
      // 提示删除成功
      this.$message.success(res.message)
      // 刷新列表
      this.getDepartments()
    },
    // 编辑部门弹框
    editDept(id) {
      this.pid = id
      this.showDialog = true
      this.isEdit = true
    },
    // 添加部门弹框 获取id 并显示弹框
    addDept(id) {
      this.pid = id
      this.showDialog = true
      this.isEdit = false
    },
    // 隐藏弹框  刷新列表  子组件通信后的事件
    updateDept() {
      this.showDialog = true
      this.getDepartments()
    },
    // 节点被点击时的回调
    // 共三个参数，依次为：传递给 data 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。
    handleNodeClick(data, node, curNode) {
      // console.log(data)
    },
    // 获取组织架构的数据
    async getDepartments() {
      // 发送请求获取部门数据
      const res = await getDepartments()
      this.originList = res.data.depts.map(item => ({
        code: item.code,
        id: item.id,
        name: item.name,
        pid: item.pid
      }))
      // console.log(res.data.depts)
      // 调用 transListToTree 方法 将获取到的扁平化数据转化为树形数据
      this.treeData = transListToTree(res.data.depts, '')
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
  .departments-tree {
    margin: 12px 0px;
  }
}
</style>
